package com.sunxd.vue.bean;

import com.sunxd.vue.domain.bean.enums.ErrorEnum;

import java.io.Serializable;
import java.util.Map;

/**
 */
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 6280855035125657876L;

    /**
     * 返回信息
     */
    private String msg;
    /**
     * code 码
     */
    private Integer code;

    /**
     * 数据
     */
    private T data;



    /**
     * 是否成功
     */
    private Boolean success;


    private Response(String msg, Integer code, T data, Boolean success) {
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.success = success;
    }

    public Response(String msg, T data, Boolean success) {
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public Response(String msg, Boolean success) {
        this.msg = msg;
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return success;
    }
    public static <R> Response<R> createSuccess(R data) {
        return createFormatMsg(200, "成功", data, Boolean.TRUE);
    }

    public static <R> Response<R> createError(String msg) {
        return createFormatMsg(500, msg, null, Boolean.FALSE);
    }

    public static <R> Response<R> createError(Integer code, String msg) {
        return createFormatMsg(code, msg, null, Boolean.FALSE);
    }

    public static <R> Response<R> createError(ErrorEnum errorEnum) {
        return createFormatMsg(errorEnum.getCode(), errorEnum.getErrMsg(), null, Boolean.FALSE);
    }

    private static <R> Response<R> createFormatMsg(Integer code, String msg, R data, Boolean success, String... args) {
        return new Response<R>(String.format(msg, args), code, data, success);
    }



}
